package com.ggh.jinjilive.ui.mall.community.adapter;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemMallCommunityBinding;
import com.ggh.jinjilive.databinding.ItemSearchBarBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener;
import com.ggh.jinjilive.mobile.mall.MallCommunityLIst;
import com.bumptech.glide.Glide;

import java.text.DecimalFormat;
import java.util.List;

public class CommunityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<MallCommunityLIst> mallCommunityLIstList;

    private int SEARCH_BAR = 1;
    private int CONTENT = 2;

    public void setAdd(List<MallCommunityLIst> mallCommunityLIstList) {
        this.mallCommunityLIstList = mallCommunityLIstList;
        notifyDataSetChanged();
    }



    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    private AddOnClickListener addOnClickListener;
    public void setAddItemOnClickListener(AddOnClickListener addOnClickListener) {
        this.addOnClickListener = addOnClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == SEARCH_BAR){
            ItemSearchBarBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_search_bar, parent, false);
            return new SearchView(binding);
        }else {
            ItemMallCommunityBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_mall_community, parent, false);
            return new ItemViewHolder(binding);
        }
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
        if(recyclerViewholder instanceof SearchView){
            SearchView holder = (SearchView) recyclerViewholder;
            //软键盘搜索
            holder.binding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        if(null != addOnClickListener){
                            addOnClickListener.onSkipClicked(v.getText().toString());
                            holder.binding.etSearch.setText("");
                        }
                        return true;
                    }
                    return false;
                }
            });

        }else if(recyclerViewholder instanceof ItemViewHolder){
            ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
            MallCommunityLIst mallCommunityLIst = mallCommunityLIstList.get(position);
            if (null != mallCommunityLIst) {
                //图片
                Glide.with(holder.binding.RoundedImageView).load(mallCommunityLIst.getImg()).into(holder.binding.RoundedImageView);
                //名称
                holder.binding.tvName.setText(mallCommunityLIst.getName());
                //
                holder.binding.tvContent.setText(mallCommunityLIst.getContent());
                //地址
                holder.binding.tvAddress.setText(mallCommunityLIst.getAddress());
                //店铺名称
                holder.binding.storeNum.setText(mallCommunityLIst.getStore_num() +"");

                //距离
                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                holder.binding.tvDistance.setText(decimalFormat.format(mallCommunityLIst.getDistance())+"km");
            }
        }

    }

    @Override
    public int getItemCount() {
        return null == mallCommunityLIstList ? 0 : mallCommunityLIstList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return SEARCH_BAR;
        }else {
            return CONTENT;
        }
    }

    class SearchView extends RecyclerView.ViewHolder {
        ItemSearchBarBinding binding;
        public SearchView(@NonNull ItemSearchBarBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemMallCommunityBinding binding;
        public ItemViewHolder(@NonNull ItemMallCommunityBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}